<?php 
    $userIds = $_GET['userIds'];
    $userId_arr = explode('|', $userIds);

    function getdevice_tokenById($userId){
        $DSN = 'mysql:dbname=user;host=127.0.0.1;port=3306;charset=UTF8';
        $pdo = new PDO($DSN, 'root', '68097777!@#');

        $state = $pdo->prepare("SELECT `device_token` FROM `users` WHERE `id` = ?");

        $state->bindValue(1, $userId, PDO::PARAM_STR);

        $state->execute();
        $state->setFetchMode(PDO::FETCH_ASSOC);
        return $state->fetch()['device_token'];
    }

    foreach ($userId_arr as $key => $userId) {
            $deviceToken = getdevice_tokenById($userId);
    //$deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b'; // 可以用你获得的DeviceToken替换  
    //$deviceToken = '165eeb8e a0b09a81 ebb1aa5c 081dd388 dbce2562 143348af 78786a35 64641477';
    $pass = '123456';   // Passphrase for the private key (ck.pem file)
       
    // Get the parameters from http get or from command line  
    $message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';  
    $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];  
    $sound = $_GET['sound'] or $sound = $argv[3];
       
    // Construct the notification payload  
    $body = array();  
    $body['aps'] = array('alert' => $message);
    if ($badge)  
      $body['aps']['badge'] = $badge;  
    if ($sound)  
      $body['aps']['sound'] = $sound;  
       
    /* End of Configurable Items */  
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', '../$$cert/ios_push_dev.pems');
    // assume the private key passphase was removed.  
    stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
    // connect to apns  
    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$fp) {
        print "Failed to connect $err $errstr\n";  
        return;  
    } else {  
       print "Connection OK\n<br/>";  
    }  
    // send message  
    $payload = json_encode($body);
    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;  
    print "Sending message :" . $payload . "\n";
    fwrite($fp, $msg);  
    fclose($fp);  
    }
 ?>